/*
 * @Author: lee
 * @Date: 2024-01-15 09:39:24
 * @LastEditors: lee
 * @LastEditTime: 2024-01-16 17:33:10
 * @Description: file content
 */
import { Notification, MessageBox, Message, Loading } from "element-ui";
// import { getToken, setToken, removeToken } from "@/utils/auth";
import { tansParams, blobValidate } from "@/utils/ruoyi";
export default function ({ $axios, redirect, app, store, $router, $route }) {
  // request interceptor
  $axios.interceptors.request.use(
    (config) => {
      // do something before request is sent
      const isToken = (config.headers || {}).isToken === false;
      let token = app.$cookies.get('Nuxt-Token')
      if (token && !isToken) {
        config.headers["Authorization"] = "Bearer " + token; // 让每个请求携带自定义token 请根据实际情况自行修改
      }
      // get请求映射params参数
      if (config.method === "get" && config.params) {
        let url = config.url + "?" + tansParams(config.params);
        url = url.slice(0, -1);
        config.params = {};
        config.url = url;
      }
      return config;
    },
    (error) => {
      // do something with request error
      console.log("axios request error", error);
      return Promise.reject(error);
    }
  );
  // response interceptor
  $axios.interceptors.response.use(
    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    (response) => {
      console.log("axios response:" + response);
      const code = response.data.code || 200;
      console.log("code", code);
      const res = response.data;
      //文件下载
      if ("business/product/downloadProductData" == response.config.url) {
        return res;
      }
      if (code === 401) {
        //过期
        // if(app.i18n.t(res.msg)) Message({type: 'error', message: app.i18n.t(res.msg)})
        store.dispatch("LogOut").then(() => {
          app.router.push(app.localePath("/"));
        });
        // return Promise.reject(new Error(res?.msg || "Auth Error"));
      } else if (code === 200) {
        //普通请求
        return res;
      } else if (code === 500) {
        if (process.browser && app.i18n.t(res?.msg))
          Message({ type: "error", message: app.i18n.t(res?.msg) });
        return Promise.reject(new Error(res?.msg || "Error"));
      } else {
        return res;
      }
    },
    (error) => {
      console.log("err" + error);
      let { message } = error;
      if (message == "Network Error") {
        //后端接口连接异常
        message = app.i18n.t("common.requestTips3");
      } else if (message.includes("timeout")) {
        // 系统接口请求超时
        message = app.i18n.t("common.overtime");
      } else if (message.includes("Request failed with status code")) {
        message =
          app.i18n.t("common.systemInterface") +
          message.substr(message.length - 3) +
          app.i18n.t("common.abnormal");
      }
      Message({ message: message, type: "error", duration: 5 * 1000 });
      return Promise.reject(error);
    }
  );

  // $axios.onRequest(config => {
  //   console.log('Making request to ' + config.url)
  // })

  // $axios.onError(error => {
  //   const code = parseInt(error.response && error.response.status)
  //   if (code === 400) {
  //     redirect('/404')
  //   } else if (code === 500) {
  //     redirect('/500')
  //   }
  // })
}
